<?php

if( !defined( '_VALID_MOS' ) && !defined( '_JEXEC' ) ) die( 'Direct Access to '.basename(__FILE__).' is not allowed.' );

/**
*
* @package MediaLibrary
* @copyright 2009 Andrey Kvasnevskiy-OrdaSoft(akbet@mail.ru);Rob de Cleen(rob@decleen.com)
* Homepage: http://www.joomlawebserver.com
* @version: 1.5.3 Basic $
*
**/


class mosMedialibraryWS{

	/**
	 * Catching information from array - in the correct language
	 * @return array that contains all Output from getWSArray() including the
	 * possibility for user to input the information directly
	 */
	function getArray(){
		$help = mosMedialibraryWS::getWSArray();
		$help1 = array();
		array_push($help1, array(0, _MEDIALIBRARY_WS_NO, ""));
		return array_merge($help1, $help);
	}


	/**
	 * Catching WS Informations array
	 * @return array containing all possible WS and their settings
	 */
	function getWSArray(){
		$retVal = array();
		array_push($retVal, array(1, _MEDIALIBRARY_AMAZON_COM, "COM"));
		array_push($retVal, array(2, _MEDIALIBRARY_AMAZON_UK, "UK"));
		array_push($retVal, array(3, _MEDIALIBRARY_AMAZON_DE, "DE"));
		array_push($retVal, array(4, _MEDIALIBRARY_AMAZON_JP, "JP"));
		array_push($retVal, array(5, _MEDIALIBRARY_AMAZON_FR, "FR"));
		array_push($retVal, array(6, _MEDIALIBRARY_AMAZON_CA, "CA"));
		return $retVal;
	}
	
	/**
	 * fetch the information from a webservice depending on the $informationFrom
	 * variable set in the $book
	 * @param medialibrary.class.php store the information that is already
	 * known of this book - $informationFrom must be set at least
	 * @return medialibrary.class.php including all information that should be
	 * added by webservices
	 * @global string $medialibrary_configuration
	 */

//***********************   add in function 'books'   *************************

	function fetchAmazonInfosForBook($media,$book) {
	    global $medialibrary_configuration;


//******************************   Added by OrdaSoft   **********************************

	    $param_ws = mosMedialibraryWS::getWsParamById($media->informationFrom);

	    //if amazon.com
	    if (($param_ws == "COM") || ($param_ws == "UK") || ($param_ws == "CA")) {
		    try{
			 if (($param_ws == "UK") || ($param_ws == "CA")) {
			    $client = new soapclient(
			 		"http://webservices.amazon.com/AWSECommerceService/".$param_ws."/AWSECommerceService.wsdl");
			 } else if (($param_ws == "COM")) {
				$client = new soapclient(
					"http://webservices.amazon.com/AWSECommerceService/AWSECommerceService.wsdl");
			        }
			$client->xml_encoding = "UTF-8";
			$params = array(
					'Request' => array(
								'SearchIndex' => 'Books',//add for isbn-13
								'IdType'=> 'ISBN',//ISBN - for isbn-13  'ASIN' - for isbn-10
								'ItemId' => $book->isbn, //'0596005431',
								'ResponseGroup' =>'Large'),//Medium
//old valid				'AWSAccessKeyId' => '1Z21K9KD9G8MAN3VWV82'
					'AWSAccessKeyId' => $medialibrary_configuration['ws']['amazon']['devtag'],
					'AssociateTag' => $medialibrary_configuration['ws']['amazon']['tag']
					);
			$result = $client->ItemLookup($params);
		    }
		    catch (SoapFault $fault) {
			$retVal = "SOAP Fault: (faultcode: {$fault->faultcode}, faultstring: {$fault->faultstring})";
			return $retVal;
//		          trigger_error("SOAP Fault: (faultcode: {$fault->faultcode}, faultstring: {$fault->faultstring})", E_USER_ERROR);
		    }
	    }

	    //if amazon. UK--yes DE--yes JP--yes(not reviews) FR--yes CA--yes
	    else {

		    try{
			$client = new soapclient(
			"http://webservices.amazon.com/AWSECommerceService/" . $param_ws . "/AWSECommerceService.wsdl");
			$client->xml_encoding = "UTF-8";
			$params = array(
					'Request' => array(
								'IdType'=> 'ASIN',
								'ItemId' => $book->isbn, //'0596005431',
								'ResponseGroup' =>'Large'),//Medium
//old valid				'AWSAccessKeyId' => '1Z21K9KD9G8MAN3VWV82'
					'AWSAccessKeyId' => $medialibrary_configuration['ws']['amazon']['devtag'],
					'AssociateTag' => $medialibrary_configuration['ws']['amazon']['tag']
					);
			$result = $client->ItemLookup($params);
		    }
		    catch (SoapFault $fault) {
			$retVal = "SOAP Fault: (faultcode: {$fault->faultcode}, faultstring: {$fault->faultstring})";
			return $retVal;
//		          trigger_error("SOAP Fault: (faultcode: {$fault->faultcode}, faultstring: {$fault->faultstring})", E_USER_ERROR);
		    }
	    }

	    //Errors test -- 1
	    if (array_key_exists('Errors', $result->OperationRequest)) {
	       $retVal = "SOAP Fault: (faultcode: {$result->OperationRequest->Errors->Error->Code}, faultstring: {$result->OperationRequest->Errors->Error->Message})";
	       return $retVal;
	    }



	    //Errors test -- 2
	    if (array_key_exists('Errors', $result->Items->Request)) {
		$retVal = "SOAP Fault: (faultcode: {$result->Items->Request->Errors->Error->Code}, faultstring: {$result->Items->Request->Errors->Error->Message})";
		return $retVal;
	    }

	    //Body -- Output in joomla form
	    //ProductName
	    $book->title = $result->Items->Item->ItemAttributes->Title;

	    //ImageUrlMedium
	    $book->imageURL = $result->Items->Item->MediumImage->URL;

	    //URL
	    $book->URL = $result->Items->Item->DetailPageURL;

	    //Manufacturer
	    if (array_key_exists('Manufacturer', $result->Items->Item->ItemAttributes)) {
	       $book->manufacturer = $result->Items->Item->ItemAttributes->Manufacturer;
	    }


	    //Author
	    $book->authors = "";
	    if ( is_array($result->Items->Item->ItemAttributes->Author) ) {		//Authors array
	       foreach ($result->Items->Item->ItemAttributes->Author as $Author) {
		       if (strlen($book->authors) > 0) {
			  $book->authors = $book->authors . ", " . $Author;
		       }
		       else { $book->authors = $Author; }
	       }
	    }
	    else {
		  $book->authors = $result->Items->Item->ItemAttributes->Author;		//Authors not array
	    }

	    //Rating
	    if (array_key_exists('CustomerReviews', $result->Items->Item)) {
	       $book->rating = ($result->Items->Item->CustomerReviews->AverageRating * 2);
	    }

	    //PublicationDate
	    if (array_key_exists('PublicationDate', $result->Items->Item->ItemAttributes)) {
	       $book->release_date = $result->Items->Item->ItemAttributes->PublicationDate;
	    }

	    //ReleaseDate
	    if (array_key_exists('ReleaseDate', $result->Items->Item->ItemAttributes)) {
	       $book->release_date = $result->Items->Item->ItemAttributes->ReleaseDate;
	    }

	    //Edition
	    if ( array_key_exists('Edition', $result->Items->Item->ItemAttributes) ) {
	       $book->edition = $result->Items->Item->ItemAttributes->Edition;
	    }

	    //Price no partner
	    if ( array_key_exists('Offer', $result->Items->Item->Offers) && array_key_exists('FormattedPrice', $result->Items->Item->Offers->Offer->OfferListing->Price)) {
	       $book->price = $result->Items->Item->Offers->Offer->OfferListing->Price->FormattedPrice;
	       $mas = $book->price;
	       $mas = ereg_replace("\xC2\xA3", "GBP ", $mas);  //for funt
	       $mas = ereg_replace("\xEF\xBF\xA5", "JPY", $mas);  //for ena
	       $book->price = $mas;
	    }
	    else {
		  $book->price = "Does not exist anymore!";
	    }

	    $book->comment = "";

	    return $book;
	}

//****************************   End of Added by Orda   *****************************


	
	/**
	 * Get the name of the WS by the id
	 * @param int $id the id of the WS-Name that should be returned
	 * @return string the name of the WS or null if it can't be found
	 */	
	function getWsNameById($id){	
		$services = mosMedialibraryWS::getArray();
		for($i=0, $n=count($services); $i < $n; $i++){
			if(intval($services[$i][0]) == intval($id)){
				return $services[$i][1];
			}		
		}	
		return null;		
	}

	/**
	 * Get the parameter of the WS by the id
	 * @param int $id the id of the WS-pram that should be returned
	 * @return string the parameter of the WS or null if it can't be found
	 */		
	function getWsParamById($id){
		$services = mosMedialibraryWS::getArray();
		for($i=0, $n=count($services); $i < $n; $i++){
			if(intval($services[$i][0]) == intval($id)){
				return $services[$i][2];
			}		
		}	
		return null;	
	}
//***********************   add in function 'music'   *************************

	function fetchAmazonInfosForMusic($media,$music) {
//******************************   Added by OrdaSoft   **********************************
	    global $medialibrary_configuration ;
	
//______________________________   Added by OrdaSoft  _____________________________

	    $param_ws = mosMedialibraryWS::getWsParamById($media->informationFrom);
	    //if amazon.com
	    if (($param_ws == "COM") || ($param_ws == "UK") || ($param_ws == "CA")) {
		    try{
			      if (($param_ws == "UK") || ($param_ws == "CA")) {
			          $client = new soapclient(
			          "http://webservices.amazon.com/AWSECommerceService/".$param_ws."/AWSECommerceService.wsdl");
			      } else if (($param_ws == "COM")) {
				      $client = new soapclient(
					      "http://webservices.amazon.com/AWSECommerceService/AWSECommerceService.wsdl");
			      }
            $client->xml_encoding = "UTF-8";
            $params = array('Request' => array('IdType'=> 'ASIN',
								  'ItemId' => $music->asin,
								  'ResponseGroup' =>'Large'
                ),
  //old valid				'AWSAccessKeyId' => '1Z21K9KD9G8MAN3VWV82'
					      'AWSAccessKeyId' => $medialibrary_configuration['ws']['amazon']['devtag'],
					      'AssociateTag' => $medialibrary_configuration['ws']['amazon']['tag']
             	 );		     
			              
    		    $result = $client->ItemLookup($params);

		      } catch (SoapFault $fault) {
			      $retVal = "SOAP Fault: (faultcode: {$fault->faultcode}, faultstring: {$fault->faultstring})";
			      return $retVal;
		        trigger_error("SOAP Fault: (faultcode: {$fault->faultcode},".
              " faultstring: {$fault->faultstring})", E_USER_ERROR);
	        }
    	}
	    //if amazon. UK--yes DE--yes JP--yes(not reviews) FR--yes CA--yes
	    else {
		    try{
			    $client = new soapclient(
			    "http://webservices.amazon.com/AWSECommerceService/" . $param_ws . "/AWSECommerceService.wsdl");
			    $client->xml_encoding = "UTF-8";
          echo "music->asin:"; exit;
          print_r($music->asin);exit;
          $params = array(
					      'Request' => array(
							      'IdType'=> 'ASIN',
							      'ItemId' => $music->asin, //'0596005431',
							      'ResponseGroup' =>'Large'),//Medium
//old valid				'AWSAccessKeyId' => '1Z21K9KD9G8MAN3VWV82'
//					'AWSAccessKeyId' => $medialibrary_configuration['ws']['amazon']['devtag'],
					      'AssociateTag' => $medialibrary_configuration['ws']['amazon']['tag']
					      );
    			$result = $client->ItemLookup($params);
		    }
		    catch (SoapFault $fault) {
			      $retVal = "SOAP Fault: (faultcode: {$fault->faultcode}, faultstring: {$fault->faultstring})";
			      return $retVal;
		        trigger_error("SOAP Fault: (faultcode: {$fault->faultcode},"
              ." faultstring: {$fault->faultstring})", E_USER_ERROR);
		    }
	    }

	    //Errors test -- 1
	    if (array_key_exists('Errors', $result->OperationRequest)) {
	        $retVal = "SOAP Fault: (faultcode: {$result->OperationRequest->Errors->Error->Code},"
            ." faultstring: {$result->OperationRequest->Errors->Error->Message})";
	       return $retVal;
	    }
	    

	    //Errors test -- 2
	    if (array_key_exists('Errors', $result->Items->Request)) {
		    $retVal = "SOAP Fault: (faultcode: {$result->Items->Request->Errors->Error->Code},".
          " faultstring: {$result->Items->Request->Errors->Error->Message})";
		    return $retVal;
	    }

//print_r($result); exit;
//		  get tracks
	    $TrackTitle = '';
	    // one cd
//      if ($result->Items->Item->ItemAttributes->NumberOfDiscs == 1)
      if ( !is_array($result->Items->Item->Tracks->Disc) )
	    {
		    for ($track = 1; $track < count ($result->Items->Item->Tracks->Disc->Track); $track++)
		    {
			    $TrackTitle .= $result->Items->Item->Tracks->Disc->Track[$track]->_;
			    $TrackTitle .= ";";	
		    }
	    }
      //array cd
      else
      {

//if we see that echo - need some check
echo "if we see that echo - need some check";

        for ($cd = 0; $cd < $result->Items->Item->ItemAttributes->NumberOfDiscs; $cd++)
        {
          $TrackTitle .="CD ".($cd+1).';';
          for ($track = 1; $track < count ($result->Items->Item->Tracks->Disc[$cd]->Track);$track++)
          {
            $TrackTitle .= $result->Items->Item->Tracks->Disc[$cd]->Track[$track]->_;
            $TrackTitle .= ';';
          }
        }
      }

			//track list
  		$music->tracklist = $TrackTitle;

	    //Price no partner
	    if ( array_key_exists('Offer', $result->Items->Item->Offers) && 
            array_key_exists('FormattedPrice', $result->Items->Item->Offers->Offer->OfferListing->Price)) {
	       $music->price = $result->Items->Item->Offers->Offer->OfferListing->Price->FormattedPrice;
	       $mas = $music->price;
	       $mas = ereg_replace("\xC2\xA3", "GBP ", $mas);  //for funt
	       $mas = ereg_replace("\xEF\xBF\xA5", "JPY", $mas);  //for ena
	       $music->price = $mas;
	    }
	    else {
	  	  $music->price = "Does not exist anymore!";
	    }
		//Rating
	  if (array_key_exists('CustomerReviews', $result->Items->Item)) {
		  $music->rating = ($result->Items->Item->CustomerReviews->AverageRating * 2);
		}
		//datetime
		//ReleaseDate
	  if (array_key_exists('ReleaseDate', $result->Items->Item->ItemAttributes)) {
		  $music->release_date = $result->Items->Item->ItemAttributes->ReleaseDate;
		}
		//binding
  	$music->binding = $result->Items->Item->ItemAttributes->Binding;
		//ImageUrlMedium
	  $music->imageURL = $result->Items->Item->MediumImage->URL;
		//URL
	  $music->URL = $result->Items->Item->DetailPageURL;
			//add artisi
	  $music->artist = $result->Items->Item->ItemAttributes->Artist;
			//add title
	  $music->title = $result->Items->Item->ItemAttributes->Title;
			//add  number_of_discs
	  $music->number_of_discs = $result->Items->Item->ItemAttributes->NumberOfDiscs;
			//add label
	  $music->label = $result->Items->Item->ItemAttributes->Label;
	    //Rating
	  if (array_key_exists('CustomerReviews', $result->Items->Item)) {
	    $music->rating = ($result->Items->Item->CustomerReviews->AverageRating * 2);
	  }
//************************   begin add for music Description   *********************
	  $music->comment = "";
//***************************** end add description  ***************************

	  return $music;
	}



//***********************   add in function 'video'   *************************

	function fetchAmazonInfosForVideo($media,$video) {
	   
//******************************   Added by OrdaSoft   **************************
	    global $medialibrary_configuration ;
	
//______________________________   Added by OrdaSoft  _____________________________

	    $param_ws = mosMedialibraryWS::getWsParamById($media->informationFrom);
	    if (($param_ws == "COM") || ($param_ws == "UK") || ($param_ws == "CA")) {
		    try{
			 if (($param_ws == "UK") || ($param_ws == "CA")) {
			    $client = new soapclient(
			 		"http://webservices.amazon.com/AWSECommerceService/".$param_ws."/AWSECommerceService.wsdl");
			 } else if (($param_ws == "COM")) {
				$client = new soapclient(
					"http://webservices.amazon.com/AWSECommerceService/AWSECommerceService.wsdl");
			        }
			$client->xml_encoding = "UTF-8";
                                $params = array('Request' => array('IdType'=> 'ASIN',
								'ItemId' => $video->asin,
								'ResponseGroup' =>'Large'
                                                            ),
  //old valid				'AWSAccessKeyId' => '1Z21K9KD9G8MAN3VWV82'
					'AWSAccessKeyId' => $medialibrary_configuration['ws']['amazon']['devtag'],
					'AssociateTag' => $medialibrary_configuration['ws']['amazon']['tag']
                                  	 );		     
			              
		         $result = $client->ItemLookup($params);

		   } catch (SoapFault $fault) {
			$retVal = "SOAP Fault: (faultcode: {$fault->faultcode}, faultstring: {$fault->faultstring})";
			return $retVal;
		          trigger_error("SOAP Fault: (faultcode: {$fault->faultcode}, faultstring: {$fault->faultstring})", E_USER_ERROR);
	 		}
	}
	    //if amazon. UK--yes DE--yes JP--yes(not reviews) FR--yes CA--yes
	   else {
		    try{
			$client = new soapclient(
			"http://webservices.amazon.com/AWSECommerceService/" . $param_ws . "/AWSECommerceService.wsdl");
			$client->xml_encoding = "UTF-8";
			$params = array(
					'Request' => array(
								'IdType'=> 'ASIN',
								'ItemId' => $video->asin, //'0596005431',
								'ResponseGroup' =>'Large'),//Medium
//old valid				'AWSAccessKeyId' => '1Z21K9KD9G8MAN3VWV82'
//					'AWSAccessKeyId' => $medialibrary_configuration['ws']['amazon']['devtag'],
					'AssociateTag' => $medialibrary_configuration['ws']['amazon']['tag']
					);
			$result = $client->ItemLookup($params);
		    }
		    catch (SoapFault $fault) {
			$retVal = "SOAP Fault: (faultcode: {$fault->faultcode}, faultstring: {$fault->faultstring})";
			return $retVal;
		          trigger_error("SOAP Fault: (faultcode: {$fault->faultcode}, faultstring: {$fault->faultstring})", E_USER_ERROR);
		    }

		}

	    //Errors test -- 1
	    if (array_key_exists('Errors', $result->OperationRequest)) {
	$retVal = "SOAP Fault: (faultcode: {$result->OperationRequest->Errors->Error->Code}, faultstring: {$result->OperationRequest->Errors->Error->Message})";
	       return $retVal;
	    }
	    

	    //Errors test -- 2
	    if (array_key_exists('Errors', $result->Items->Request)) {
		$retVal = "SOAP Fault: (faultcode: {$result->Items->Request->Errors->Error->Code}, faultstring: {$result->Items->Request->Errors->Error->Message})";
		return $retVal;
	    }


		//Title
	$video->title = $result->Items->Item->ItemAttributes->Title;

		//imageURL
	$video->imageURL = $result->Items->Item->MediumImage->URL;

		//URL
	$video->URL = $result->Items->Item->DetailPageURL;

 		//Rating
	$video->rating = ($result->Items->Item->CustomerReviews->AverageRating * 2);

		//Actors
	$Actor = '';
	if (is_array($result->Items->Item->ItemAttributes->Actor))
	{
		for ($i = 0; $i<count($result->Items->Item->ItemAttributes->Actor); $i++)
		{
			if ($i) $Actor.=', ';
			$Actor.=$result->Items->Item->ItemAttributes->Actor[$i];
		}
	}
	else 
	{
		$Actor = $result->Items->Item->ItemAttributes->Actor;
	}
	if ($Actor != '')$video->actors = $Actor;

	//comments
	$video->comment = "";

	//Language
	$Language = null;
	if (IS_ARRAY($result->Items->Item->ItemAttributes->Languages->Language))
	{
		for ($i = 0; $i < count ($result->Items->Item->ItemAttributes->Languages->Language); $i++)
		{
			if ($i)  $Language.=', ';
			$Language.=$result->Items->Item->ItemAttributes->Languages->Language[$i]->Name.'('.$result->Items->Item->ItemAttributes->Languages->Language[$i]->Type.')';
		}
	}
	else 
	{
		$Language = $result->Items->Item->ItemAttributes->Languages->Language->Name;
	}
	$video->language = $Language;

		//ReleaseDate
	$video->release_date = $result->Items->Item->ItemAttributes->ReleaseDate;

	//Price no partner
	if ( array_key_exists('Offer', $result->Items->Item->Offers) 
        && array_key_exists('FormattedPrice', $result->Items->Item->Offers->Offer->OfferListing->Price)) {
	    $video->price = $result->Items->Item->Offers->Offer->OfferListing->Price->FormattedPrice;
	    $mas = $video->price;
	    $mas = ereg_replace("\xC2\xA3", "GBP ", $mas);  //for funt
	    $mas = ereg_replace("\xEF\xBF\xA5", "JPY", $mas);  //for ena
	    $video->price = $mas;
	}
	else {
		$video->price = "Does not exist anymore!";
	}

		//NumberOfItems
	if ($result->Items->Item->ItemAttributes->NumberOfItems)
	{
		$video->number_of_discs = $result->Items->Item->ItemAttributes->NumberOfItems;
	}

		//RunningTime
	if ($result->Items->Item->ItemAttributes->RunningTime)
	{
		$video->run_time = $result->Items->Item->ItemAttributes->RunningTime->_.' '.$result->Items->Item->ItemAttributes->RunningTime->Units;
	}
	
		//Studio
	if ($result->Items->Item->ItemAttributes->Studio)
	{
		$video->studio = $result->Items->Item->ItemAttributes->Studio;
	}

//print_r($result); exit;
		//producers
	$Producers = '';
	$first = 0;
  if ( array_key_exists('Creator', $result->Items->Item->ItemAttributes ) ) {
    if (IS_ARRAY($result->Items->Item->ItemAttributes->Creator))
    {
      for($i = 0; $i < count ($result->Items->Item->ItemAttributes->Creator);$i++)
      {
        if ($result->Items->Item->ItemAttributes->Creator[$i]->Role=='Producer')
        {
          if (!$first) $first = 1;
          else $Producers.=', ';
          $Producers .= $result->Items->Item->ItemAttributes->Creator[$i]->_;
        }
      }
    }
    else if ($result->Items->Item->ItemAttributes->Creator->Role=='Producer')
    {
      $Producers = $result->Items->Item->ItemAttributes->Creator->_;
    }
    if ($Producers != '') $video->producers = $Producers;
  }
		//directors
	$Directors = '';
  if ( array_key_exists('Director', $result->Items->Item->ItemAttributes ) ) 
	{
		if (IS_ARRAY($result->Items->Item->ItemAttributes->Director))
		{
			$Directors = implode(',', $Directors = $result->Items->Item->ItemAttributes->Director);
		}
		else
		{
			$Directors = $result->Items->Item->ItemAttributes->Director;	
		}
	}
	$video->directors = $Directors;
//print_r($result->Items->Item->ItemAttributes); exit;

		//format
	if ($result->Items->Item->ItemAttributes->Format)
	{
		$format = '';
		if (is_array($result->Items->Item->ItemAttributes->Format))
		{
			for ($i = 0; $i < count ($result->Items->Item->ItemAttributes->Format); $i++)
			{
				if ($i) {$format.=', ';}
				$format.=$result->Items->Item->ItemAttributes->Format[$i];
			}
		}
		else
		{
			$format = $result->Items->Item->ItemAttributes->Format;
		}
		$video->format = $format;
	}
		//Region code\
	if ($result->Items->Item->ItemAttributes->RegionCode)
	{
		$region = $result->Items->Item->ItemAttributes->RegionCode;
		switch ($region)
		{
		case 0:
			$region.=' ('._REGION0.')';
		  break;
		case 1:
			$region.=' ('._REGION1.')';
		  break;
		case 2:
			$region.=' ('._REGION2.')';
		  break;
		case 3:
			$region.=' ('._REGION3.')';
		  break;
		case 4:
			$region.=' ('._REGION4.')';
		  break;
		case 5:
			$region.=' ('._REGION5.')';
		  break;
		case 6:
			$region.=' ('._REGION6.')';
		  break;
		case 8:
			$region.=' ('._REGION8.')';
		  break;
		}
	}
	$video->region = $region;
	return $video;
	}


	//***********************   add in function 'games'   *************************

	function fetchAmazonInfosForGame($media,$game) {
	   
	//******************************   Added by OrdaSoft   **************************
	    global $medialibrary_configuration ;
	
//______________________________   Added by OrdaSoft  _____________________________

	    $param_ws = mosMedialibraryWS::getWsParamById($media->informationFrom);
	    if (($param_ws == "COM") || ($param_ws == "UK") || ($param_ws == "CA")) {
		    try{
			 if (($param_ws == "UK") || ($param_ws == "CA")) {
			    $client = new soapclient(
			 		"http://webservices.amazon.com/AWSECommerceService/".$param_ws."/AWSECommerceService.wsdl");
			 } else if (($param_ws == "COM")) {
				$client = new soapclient(
					"http://webservices.amazon.com/AWSECommerceService/AWSECommerceService.wsdl");
			        }
			$client->xml_encoding = "UTF-8";
                                $params = array('Request' => array('IdType'=> 'ASIN',
								'ItemId' => $game->asin,
								'ResponseGroup' =>'Large'
                                                            ),
  //old valid				'AWSAccessKeyId' => '1Z21K9KD9G8MAN3VWV82'
					'AWSAccessKeyId' => $medialibrary_configuration['ws']['amazon']['devtag'],
					'AssociateTag' => $medialibrary_configuration['ws']['amazon']['tag']
                                  	 );		     
			              
		         $result = $client->ItemLookup($params);

		   } catch (SoapFault $fault) {
			$retVal = "SOAP Fault: (faultcode: {$fault->faultcode}, faultstring: {$fault->faultstring})";
			return $retVal;
		          trigger_error("SOAP Fault: (faultcode: {$fault->faultcode}, faultstring: {$fault->faultstring})", E_USER_ERROR);
	 		}
	}
	    //if amazon. UK--yes DE--yes JP--yes(not reviews) FR--yes CA--yes
	   else {
		    try{
			$client = new soapclient(
			"http://webservices.amazon.com/AWSECommerceService/" . $param_ws . "/AWSECommerceService.wsdl");
			$client->xml_encoding = "UTF-8";
			$params = array(
					'Request' => array(
								'IdType'=> 'ASIN',
								'ItemId' => $game->asin, //'0596005431',
								'ResponseGroup' =>'Large'),//Medium
//old valid				'AWSAccessKeyId' => '1Z21K9KD9G8MAN3VWV82'
//					'AWSAccessKeyId' => $medialibrary_configuration['ws']['amazon']['devtag'],
					'AssociateTag' => $medialibrary_configuration['ws']['amazon']['tag']
					);
			$result = $client->ItemLookup($params);
		    }
		    catch (SoapFault $fault) {
			$retVal = "SOAP Fault: (faultcode: {$fault->faultcode}, faultstring: {$fault->faultstring})";
			return $retVal;
		          trigger_error("SOAP Fault: (faultcode: {$fault->faultcode}, faultstring: {$fault->faultstring})", E_USER_ERROR);
		    }

		}

	    //Errors test -- 1
	    if (array_key_exists('Errors', $result->OperationRequest)) {
	$retVal = "SOAP Fault: (faultcode: {$result->OperationRequest->Errors->Error->Code}, faultstring: {$result->OperationRequest->Errors->Error->Message})";
	       return $retVal;
	    }
	    

	    //Errors test -- 2
	    if (array_key_exists('Errors', $result->Items->Request)) {
		$retVal = "SOAP Fault: (faultcode: {$result->Items->Request->Errors->Error->Code}, faultstring: {$result->Items->Request->Errors->Error->Message})";
		return $retVal;
	    }


		    //Price no partner
	    if ( array_key_exists('Offer', $result->Items->Item->Offers) && array_key_exists('FormattedPrice', $result->Items->Item->Offers->Offer->OfferListing->Price)) {
	       $game->price = $result->Items->Item->Offers->Offer->OfferListing->Price->FormattedPrice;
	       $mas = $game->price;
	       $mas = ereg_replace("\xC2\xA3", "GBP ", $mas);  //for funt
	       $mas = ereg_replace("\xEF\xBF\xA5", "JPY", $mas);  //for ena
	       $video->price = $mas;
	    }
	    else {
		  $game->price = "Does not exist anymore!";
	    }

	 	//Rating
	if ($result->Items->Item->CustomerReviews->AverageRating)
	{
		$game->rating = ($result->Items->Item->CustomerReviews->AverageRating * 2);
	}
	    //ProductName
	    $game->title = $result->Items->Item->ItemAttributes->Title;

	    //ImageUrlMedium
	    $game->imageURL = $result->Items->Item->MediumImage->URL;

	    //URL
	    $game->URL = $result->Items->Item->DetailPageURL;

		//comment
	$game->comment = "";

		//ReleaseDate
	$game->release_date = $result->Items->Item->ItemAttributes->ReleaseDate;

		//studio
	$game->studio = $result->Items->Item->ItemAttributes->Studio;
	
		//binding
	$game->binding = $result->Items->Item->ItemAttributes->Binding;

			//Platform
	$Platform = '';
	if (IS_ARRAY($result->Items->Item->ItemAttributes->Platform))
	{
		$Platform = implode(', ', $result->Items->Item->ItemAttributes->Platform);
	}
	else if ($result->Items->Item->ItemAttributes->Platform)
	{
		$Platform = $result->Items->Item->ItemAttributes->Platform;
	}
	$game->platform = $Platform;
	return $game;
	}
}
?>
